Unions in Odin
The union data type in Odin. Includes (1) basic grammar; (2) type assertion; (3) type switch on union; (4) union tag. Last updated version: dev-2026-10
Table of Contents
1. Type Assertion
We can use the following statements to check if the data stored in the union is of a certain type. Returns value and ann optional verdict.
v :: union {
string,
int,
bool,
}
// type assertion. Panic if 'v' does not contain a string
s1 := v.(string)
// type assertion but explicitly unpack the verdict. It won't panic.
s2, ok := v.(string)
2. Type Switch
We can use switch case statement on a union based on the type it stores.
switch v in value {
case string:
#assert(type_of(v) == string)
case bool:
#assert(type_of(v) == bool)
case: // Default branch
// v = nil
}